﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.Recycling.Object;
using Imaginecup.Recycling.GameMan;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Imaginecup.recycling.Object.Entity;
using Imaginecup.shareData;
using Imaginecup.util.Funtion;

namespace Imaginecup.Recycling.Behavior
{
    public class Run : IMovingBehavior
    {
        private int speed = 2;
        private bool loopFlag = true;
        private ObjectInfo Info;
        private Viewport m_Viewport;
        private Direction m_Direction = Direction.Right;
        public Run(ObjectInfo Info)
        {
            this.Info = Info;
            this.m_Viewport = StateShareData.GetShareData().GetGame().GraphicsDevice.Viewport;

            speed = UserRandom.rand.Next(5)+1;
        }

        #region RunBehavior 각 종 셋팅 함수
        public void SetDirection(Direction direction)
        {
            m_Direction = direction;
        }

        public void SetSpeed(int speed)
        {
            this.speed = speed;
        }
        public void SetLoopFlag(bool flag)
        {
            this.loopFlag = flag;
        }
        #endregion
        

        public void UpdateMove(GameTime gameTime)
        {
            if ((m_Direction & Direction.Right) != 0) // 오른쪽 이동일 때 
            {
                Info.SetPositionX(Info.WorldLocation.X + speed);
                // 캐릭터가 화면 오른쪽 바깥으로 나가는 것을 막기 위함
                if(loopFlag)
                {
                    if (Info.WorldLocation.X > m_Viewport.Width + Info.FrameWidth)
                        ChangePosition(-200);
                }
            }
            else if ((m_Direction & Direction.Left) != 0) // 왼쪽 이동일 때
            {
                Info.SetPositionX(Info.WorldLocation.X - speed);

                // 캐릭터가 화면 왼쪽 바깥으로 나가는 것을 막기 위함
                if (loopFlag)
                {
                    if (Info.WorldLocation.X < 0 - Info.FrameWidth)
                        ChangePosition(m_Viewport.Width);
                }
            }
        }

        private void ChangePosition(float x)
        {
            Info.SetPositionX(x);
            Info.SetPositionY(UserRandom.rand.Next(50) + 10.0f);
            SetSpeed(UserRandom.rand.Next(5) +1);
            
        }


        public bool GetLoopFlag()
        {
            return loopFlag;
        }
    }
}
